//
//  AppDelegate+JPush.m
//  CoreJPush
//
//  Created by 冯成林 on 15/9/17.
//  Copyright (c) 2015年 冯成林. All rights reserved.
//

#import "AppDelegate+JPush.h"
#import "JPUSHService.h"
#import "CoreJPush.h"

@implementation AppDelegate (JPush)


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[EMClient sharedClient] bindDeviceToken:deviceToken];
    });
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
    if (EXRACT_OBJECT(NEWSCOUNT) == nil)
    {
        SAVE_OBJECT(@"1", NEWSCOUNT);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"news" object:nil];
    }
    else
    {
        NSString *count = [NSString stringWithFormat:@"%i",[EXRACT_OBJECT(NEWSCOUNT) intValue] +1];
        SAVE_OBJECT(count, NEWSCOUNT);
    }
    
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
                                                        options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *str =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.content", @"Apns content")
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
                                          otherButtonTitles:nil];
    [alert show];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    

    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    CoreJPush *jpush = [CoreJPush sharedCoreJPush];
    [jpush didReceiveRemoteNotification:userInfo];
    
    NSLog(@"消息数：%@",EXRACT_OBJECT(NEWSCOUNT));
    if (EXRACT_OBJECT(NEWSCOUNT) == nil)
    {
        SAVE_OBJECT(@"1", NEWSCOUNT);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"news" object:nil];
    }
    else
    {
        NSString *count = [NSString stringWithFormat:@"%i",[EXRACT_OBJECT(NEWSCOUNT) intValue] +1];
        SAVE_OBJECT(count, NEWSCOUNT);
    }
    
    if (userInfo[@"aps"] && userInfo[@"aps"][@"alert"]) {
        [[AlertViewManager shareManager] initWithMessage:userInfo[@"aps"][@"alert"] cancleButtonTitle:@"确定" clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
            
        }];
    }
}


@end
